package com.ruoyi.service.impl;


import com.ruoyi.domain.dao.BookMark;
import com.ruoyi.mapper.BookMarkMapper;
import com.ruoyi.service.BookMarkService;
import com.ruoyi.myutils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @auther: zxw
 * @Description //TODO改动挺大
 * @date: 2024/11/16 上午11:04
 */
@Service("BookMarkService")
public class BookMarkServiceImpl implements BookMarkService {

    @Autowired
    private BookMarkMapper bookMarkRepository;

    @Value("${file.bookmark}")
    private String bookMarkFolder;

    @Override
    public List getBookMarkInfo() {
        List<BookMark> bookMarks = bookMarkRepository.getBookMarkInfo();
        return bookMarks;
    }

    @Override
    public void addBookMarkInfo(MultipartFile multipartFile, BookMark bookMark) {
        try {
            File bookMarkFolder1 = new File(bookMarkFolder);
            if (!bookMarkFolder1.isDirectory()) bookMarkFolder1.mkdirs();
            File file = FileUtil.upload(multipartFile, bookMarkFolder);
            bookMark.setCreate_time(LocalDateTime.now());
            String base64 = "'" + FileUtil.fileToBase64(file) + "'";
            bookMark.setBase64(base64);
            bookMarkRepository.addBookMark(bookMark);

        } catch (Exception e) {
            e.printStackTrace();

        }

    }

    @Override
    public void delBookMark(Integer id) {
        bookMarkRepository.deleteBookMark(id);
    }

    @Override
    public List<BookMark> getInfoByName(String name) {
        BookMark bookMark = BookMark.builder().name(name).build();
        return bookMarkRepository.getInfoByName(bookMark);
    }
    //新增  删除 查询全部
}
